package co.com.jtune.persistence.localPersistence;

import java.util.ArrayList;
import java.util.List;
import java.util.prefs.Preferences;

import co.com.jtune.model.TrackModel;
import co.com.jtune.persistence.TrackPersistence;

/**
 * Clase singleton que se encarga de almacenar localmente las canciones
 * haciendo uso de la libreria {@link Preferences}.
 * 
 * Para mas info sobre Preferences: {@linkplain http://alvinalexander.com/blog/post/java/simple-java-preferences-api-example}
 * @author agutierrez
 *
 */
public class LocalPersistenceTrackImpl implements TrackPersistence{
	
	private static LocalPersistenceTrackImpl instance;
	
	private static final String TRACKS_COUNT = "TRACKS_COUNT";
	
	private static final String TRACK_NAME = "TRACK_NAME_";
	private static final String TRACK_ARTIST = "TRACK_ARTIST_";
	private static final String TRACK_ALBUM = "TRACK_ALBUM_";
	private static final String TRACK_DURATION = "TRACK_DURATION_";
	private static final String TRACK_GENER = "TRACK_GENER_";
	private static final String TRACK_LOCATION = "TRACK_LOCATION_";
	
	private Preferences preferences;
	private long tracksCount;
	
	private LocalPersistenceTrackImpl(Preferences preferences)
	{
		if(preferences == null)
		{
			preferences = Preferences.userRoot().node(getClass().getName());;
		}
		
		this.preferences = preferences;
		tracksCount = loadTracksCount();
	}
	
	public static LocalPersistenceTrackImpl getTrackPersistenceInstance()
	{
		if(instance == null)
			instance = new LocalPersistenceTrackImpl(null);
		return instance;
	}
	
	public static LocalPersistenceTrackImpl getTrackPersistenceInstance(Preferences preferences)
	{
		if(instance == null)
			instance = new LocalPersistenceTrackImpl(preferences);
		return instance;
	}
	
	/**
	 * {@inheritDoc}
	 */
	public Long saveTrack(TrackModel track)
	{
		tracksCount ++;
		saveTracksCount();
		
		preferences.put(TRACK_NAME + tracksCount, track.getName());
		preferences.put(TRACK_ARTIST + tracksCount, track.getArtist());
		preferences.put(TRACK_ALBUM + tracksCount, track.getAlbum());
		preferences.putLong(TRACK_DURATION + tracksCount, track.getDuration());
		preferences.put(TRACK_GENER + tracksCount, track.getGenre());
		preferences.put(TRACK_LOCATION + tracksCount, track.getLocation());
		
		return Long.valueOf(tracksCount);
	}
	
	/**
	 * {@inheritDoc}
	 */
	public void saveTrackList(List<TrackModel> trackList)
	{
		for (TrackModel track : trackList) {
			saveTrack(track);
		}
	}
	
	/**
	 * {@inheritDoc}
	 */
	public List<TrackModel> loadTrackList()
	{
		List<TrackModel> trackList = new ArrayList<TrackModel>();
		
		for (int i = 1; i <= tracksCount; i++) {
			trackList.add(loadTrack(i));
		}
		
		return trackList;
	}
	
	private TrackModel loadTrack(long id)
	{
		TrackModel track = new TrackModel("","");
		
		track.setName(preferences.get(TRACK_NAME + id, ""));
		track.setArtist(preferences.get(TRACK_ARTIST + id, ""));
		track.setAlbum(preferences.get(TRACK_ALBUM + id, ""));
		track.setDuration(preferences.getLong(TRACK_DURATION + id, 0));
		track.setGenre(preferences.get(TRACK_GENER + id, ""));
		track.setLocation(preferences.get(TRACK_LOCATION + id, ""));
		
		return track;
	}
	
	private long loadTracksCount()
	{
		return preferences.getInt(TRACKS_COUNT, 0);
	}
	
	private void saveTracksCount()
	{
		preferences.putLong(TRACKS_COUNT, tracksCount);
	}

}
